package com.zxw.article.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.zxw.article.service.NoticeService;
import com.zxw.common.api.ApiResult;
import com.zxw.common.api.PageResult;
import com.zxw.common.constant.ApiConst;
import com.zxw.common.entity.notice.NoticeEntity;
import com.zxw.common.entity.notice.NoticeListQuery;
import com.zxw.common.entity.notice.NoticeQuery;
import com.zxw.common.entity.notice.NoticeVO;
import com.zxw.common.enums.ResultCode;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;


/**
 * <p>
 * 公告接口
 * </p>
 *
 * @author zhanxuewei
 * @since 2022-09-20
 */
@Api(tags = "公告接口", description = "NoticeController")
@RestController
@RequestMapping(ApiConst.API_PREFIX +"/notice")
public class NoticeController {

    @Autowired
    private NoticeService noticeService;

    @ApiOperation("添加")
    @PostMapping("/save")
    public ApiResult<ResultCode> save(@RequestBody NoticeQuery noticeQuery) {
        noticeService.save(noticeQuery);
        return ApiResult.success(ResultCode.SUCCESS);
    }

    @ApiOperation("列表条件分页查询")
    @GetMapping("/pageList")
    public ApiResult<PageResult<NoticeVO>> pageList(NoticeListQuery noticeListQuery) {
        IPage<NoticeVO> iPage = noticeService.pageList(noticeListQuery);
        return ApiResult.success(PageResult.restPage(iPage, iPage.getRecords()));
    }

    @ApiOperation("批量添加")
    @PostMapping("/saveBatch")
    public ApiResult<ResultCode> saveBatch(@RequestBody List<NoticeQuery> list) {
        noticeService.saveBatch(list);
        return ApiResult.success(ResultCode.SUCCESS);
    }

    @ApiOperation("修改")
    @PostMapping("/update")
    public ApiResult<ResultCode> update(@RequestBody NoticeQuery noticeQuery) {
        noticeService.updateById(noticeQuery);
        return ApiResult.success(ResultCode.SUCCESS);
    }

    @ApiOperation("批量删除")
    @PostMapping("/deletes")
    public ApiResult<ResultCode> deletes(@RequestBody List<String> ids) {
        noticeService.removeByIds(ids);
        return ApiResult.success(ResultCode.SUCCESS);
    }

    @ApiOperation("详情")
    @GetMapping("/detail/{id}")
    public ApiResult<NoticeVO> detail(@PathVariable("id") String id) {
        NoticeEntity noticeEntity = noticeService.getById(id);
        NoticeVO noticeVO = new NoticeVO();
        BeanUtils.copyProperties(noticeEntity, noticeVO);
        return ApiResult.success(noticeVO);
    }

}

